<?php

namespace app\api\controller;

use app\admin\model\store\Servec;
use app\admin\model\technician\CheckUser;
use app\admin\model\technician\Technicianlabel;
use app\admin\model\time\Quantum;
use app\common\controller\Api;
use app\common\model\Addclock;
use app\common\model\BusyHours;
use app\common\model\CommentZan;
use app\common\model\Config;
use app\common\model\Follow;
use app\common\model\MoneyLog;
use app\common\model\OrderComment;
use app\common\model\TechnicianServe;
use app\common\model\Withdraw;
use think\Cache;
use think\Db;

class Technician  extends Api
{
    protected $noNeedLogin = ['technician_time','getServeTime'];
    protected $noNeedRight = ['*'];


    /**
     * 关注技师/取消关注
     */
    public function technician_follow(){
        $uid           = $this->auth->id;
        $type          = $this->request->request('type');//1关注技师 2取消关注
        $postion=$this->auth->postion;
        if($postion!=1){
            $this->error('当前身份不是用户');
        }
        $technician_id = $this->request->request('technician_id');
        if($type==1){
$technician_info=\app\common\model\Technician::get($technician_id);
            $old_data        = Follow::where(['uid' => $uid, 'technician_id' => $technician_id])->find();
            if($old_data) {
                if ($old_data['follow_status'] == 1) {
                    $this->error('您已关注当前技师');
                } else {
                    $res = $old_data->save(['follow_status' => 1]);
                }
            }else{
                $data = [
                    'uid'               => $uid,
                    'technician_id'     => $technician_id,
                    'technician_avater' => $technician_info['technician_avatar'],
                    'follow_status'     => 1,
                    'createtime'        => time(),
                    'follow_time'=>time(),
                     'type'=>1
                ];
                $res  = Follow::create($data);
            }
            \app\common\model\User::where('id', $uid)->setInc('technician_follow');
        }else if($type==2){
            $technician_info = Follow::where(['uid' => $uid, 'technician_id' => $technician_id, 'follow_status' => 1])->find();
            if(!$technician_info){
                $this->error('您未关注当前技师');
            }
            $res    = $technician_info->save(['follow_status' =>2]);
            \app\common\model\User::where('id', $uid)->setDec('technician_follow');
        }
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

  /**
   * @throws \think\Exception
   * @throws \think\db\exception\DataNotFoundException
   * @throws \think\db\exception\ModelNotFoundException
   * @throws \think\exception\DbException
   * 技师详情
   */
    public function technician_detail(){
      $uid=$this->auth->id;
      $id   = $this->request->request('id');
      $lon = $this->request->request('lon',0);
      $lat  = $this->request->request('lat',0);

      if (empty($lon) || empty($lat)) {
        $lon = 0;
        $lat  = 0;
      }
      if ($lat == 'undefined') {
        $lat = 0;
      }
      if ($lon == 'undefined') {
        $lon = 0;
      }
      $technician=\app\common\model\Technician::where(['technician_id'=>$id])->field('technician_name,technician_avatar,technician_age,technician_gender,technician_images,service_area,nation,height,weight,introduction,lon,lat,education_status,constellation,zodiak,health_cards,license,other_images,feedback,store_id,technician_label')->find();
      if( $technician){
        $store                       = Db::name('store')->where(['store_id' => $technician->store_id])->field('store_id,name')->find();
        $follow_status=Follow::where(['uid'=>$uid,'technician_id'=>$id,'follow_status'=>1])->find();
        if($follow_status){
          $technician['follow_status']='已关注';
        }else{
          $technician['follow_status']='未关注';
        }
        $label=Db::name('technicianlabel')->where('id','=',$technician['technician_label'])->find();
        $technician['label']=$label['technician_label'];
        if($technician['education_status']==0){
          $technician['education_status']='保密';
        }else if($technician['education_status']==1){
          $technician['education_status']='大专';
        }else if($technician['education_status']==2) {
          $technician['education_status'] = '本科';
        }
        else if($technician['education_status']==3) {
          $technician['education_status'] = '研究生';
        }else if($technician['education_status']==4) {
          $technician['education_status'] = '博士';
        }else if($technician['education_status']==5) {
          $technician['education_status'] = '博士后';
        }
        $technician['health_cards']=explode(',',$technician['health_cards']);
        $technician['other_images']=explode(',',$technician['other_images']);
        $technician['technician_images']=explode(',',$technician['technician_images']);
        $technician['store']=$store;
        $technician['distance']=distance($lon,$lat, $technician->lon, $technician->lat);
        //关注数量:
        $follow_count=Follow::where(['technician_id'=>$id])->count();
        $technician['follow_count']=$follow_count;
        //可约时间
        $isset_order=\app\common\model\Order::where(['technician_id'=>$id,'order_status'=>['IN', [2,3,4,5]]])->find();
        if($isset_order){
          $earliestappionent_time = date('H:i', $isset_order['endserve_time']);
        }else{
          $earliestappionent_time = date('H:i', time());
        }
        $technician['earliestappionent_time']=$earliestappionent_time;
        $serve               = Db::name('technician_serve')
          ->alias('ts')
          ->join('serve s','s.id=ts.serve_id')
          ->where('s.serve_status','=',1)
          ->where('ts.technician_id','=',$id)
          ->order('s.weigh desc')
          ->field('s.id,s.serve_name,s.serve_image,s.serve_duration,s.serve_price,s.sales,s.serve_details,s.feedback')
          ->select();
        $technician['serve'] = $serve;
        $comment_count=OrderComment::where(['technician_id'=>$id])->count();
        $technician['comment_count']=$comment_count;

      }
      $this->success('请求成功',$technician);
    }


    /**
     * 技师介绍
     */

    public function technician_introuce(){
      $uid=$this->auth->id;
      $serve_id=$this->request->request('serve_id');
      $lon=$this->request->request('lon');
      $lat=$this->request->request('lat');

      if(!$serve_id){
        $this->error('参数错误');
      }
      $tserve=TechnicianServe::where(['serve_id'=>$serve_id])->group('technician_id')->column('technician_id');
      $technician=\app\common\model\Technician::where(['technician_id'=>['in',$tserve]])->field('technician_avatar,technician_id,technician_age,technician_gender,technician_name,real_name,education_status,nation,height,weight,introduction,zodiak,constellation,comment_count,feedback,technician_images,lon,lat,technician_address,license')->select();
     if( $technician) {
         foreach ($technician as $item) {
             $isset_order = \app\common\model\Order::where(['technician_id' =>$item['technician_id'], 'order_status' => ['IN', [2, 3, 4, 5]]])->find();
             if ($isset_order) {
                 $item['earliestappionent_time'] = date('H:i', $isset_order['endserve_time']);
             } else {
                 $item['earliestappionent_time'] = date('H:i', time());
             }
             $item['distance']=distance($lon,$lat,$item['lon'],$item['lat']);
             if ($item['education_status'] == 0) {
                 $item['education_status'] = '保密';
             } else if ($item['education_status'] == 1) {
                 $item['education_status'] = '大专';
             } else if ($item['education_status'] == 2) {
                 $item['education_status'] = '本科';
             } else if ($item['education_status'] == 3) {
                 $item['education_status'] = '研究生';
             } else if ($item['education_status'] == 4) {
                 $item['education_status'] = '博士';
             } else if ($item['education_status'] == 5) {
                 $item['education_status'] = '博士后';
             }
             $item['technician_images'] = explode(',', $item['technician_images']);
             $item['technician_address'] = mb_substr($item['technician_address'],0,2);
             $follow_status=Follow::where(['uid'=>$uid,'technician_id'=>$item['technician_id'],'follow_status'=>1])->find();
             if($follow_status){
                 $item['follow_status']='已关注';
             }else{
                 $item['follow_status']='未关注';
             }
         }
     }
      $this->success('查询成功',$technician);
    }

  /**
   * 查看当前技师实名认证情况
   */

  public function showcheckuser(){
    $technician_id=$this->request->request('technician_id');
    $technician=\app\common\model\Technician::get($technician_id);
    $checkuser=CheckUser::where(['uid'=>$technician['uid']])->field('id,real_name,idno')->find();
    if($checkuser){
      $check='已认证';
    }else{
      $check='未认证';
    }
    $store_id=$technician['store_id'];
    $store=\app\common\model\Store::get($store_id);
    $checkuser['store_license']=$store['lincense'];
    $checkuser['technician_license']=$technician['license'];
    $checkuser['check']=$check;
    $this->success('',$checkuser);

  }


    /**
     * 直约技师列表
     */

    public function technician_list(){
      $serve_id = $this->request->request('serve_id');//当前服务
      $page     = $this->request->request('page',1);//页数
      $pageSize=10;//条数
      $serve_time=$this->request->request('serve_time');//服务时间
      $lon     = $this->request->request('lon',0);//经度
      $lat     = $this->request->request('lat',0);//纬度
      $distance=$this->request->request('distance');//距离
      $sex      = $this->request->request('sex');//性别
      $order    = $this->request->request('order');//人气高
      $age_str    = $this->request->request('age');//最低年龄
//      $age1   = $this->request->request('age1');//最高年龄
        $times = strtotime($serve_time);
        $timey=datetime($times,'H:i');
        $week = date('w', $times);
      $store_name = $this->request->request('store_name');//店铺名称
      $name = $this->request->request('name','');//技师姓名
      if (empty($lon) || empty($lat)) {
        $lon = 0;
        $lat  = 0;
      }
      if ($lat == 'undefined') {
        $lat = 0;
      }
      if ($lon == 'undefined') {
        $lon = 0;
      }

      $where = [];
      if ($serve_id) {
          $servec = Servec::where(['id' => $serve_id])->find();
          if (!$servec) {
              $this->error('服务类型错误');
          }
          // 获取服务集合
          $serve_arr              = \app\admin\model\Serve::where(['serve_cid' =>$serve_id,'serve_status'=>1,'is_added'=>0])->column('id');
          $technician_ids         = TechnicianServe::where('serve_id','in',$serve_arr)->group('technician_id')->column('technician_id');
          $where['technician_id'] = ['in',$technician_ids];
      }
      if($age_str){
          list($age,$age1) = explode('-',$age_str);
          $where['technician_age']=["between",[$age,$age1]];
      }

      if ($sex) {
        if ($sex == 2) {
          $sex = 0;
        }
        $where['technician_gender'] = $sex;
      }
     if($serve_time){
         $where['work_day']=['like',"%$week%"];
       $where['serve_time'] = ['<',$timey];
         $where['serve_time_end'] = ['>=',$timey];
     }

      if ($order) {
        if ($order == 1) {
          $order = 'order_count desc';
        } else {
          $order = 'order_count asc';
        }
      }
      if($store_name){

        $store=\app\common\model\Store::where(['name'=>['like',"%$store_name%"]])->find();
        $where['store_id']=$store->store_id??0;
        $order             = 'order_count desc';
      }
//      if ($store_id) {
//        $where['store_id'] = $store_id;
//        $order             = 'order_count desc';
//      }
      if($name){
        $where['technician_name']=['like',"%$name%"];
      }
      $technician            = \app\common\model\Technician::where($where)->order($order)->page($page,$pageSize)->field('technician_id,technician_name,technician_avatar,technician_gender,technician_age,store_id,introduction,lon,lat,service_area,technician_address')->select();
     foreach ($technician as $k=>$v){
       $isset_order=\app\common\model\Order::where(['technician_id'=>$v['technician_id'],'order_status'=>['IN', [2,3,4,5]]])->find();
       if($isset_order){
         $v['earliestappionent_time'] = date('H:i', $isset_order['endserve_time']);
       }else{
         $v['earliestappionent_time'] = date('H:i', time());
       }
         $v['distance'] = distance($lon,$lat, $v['lon'], $v['lat']);//距离我km
         $store=\app\common\model\Store::where(['store_id'=>$v['store_id']])->field('store_id,name')->find();
         $v['store']=$store;
     }
      if($technician){
        if($distance) {
          array_multisort(array_column($technician, 'distance'), SORT_ASC, $technician_list);
        }
        }
      $this->success('请求成功', $technician);
    }


    /**
     * 技师时间
     */


   public  function technician_time(){


       $arr    = [];
           for($i = 0; $i < 48; $i++){
                   $beginTime = mktime(0, 0, 0, date("m"), date("d"), date("y"));
                   if(intval(datetime(time(), "H")) * 2  <= $i){
                       $b     = $beginTime + ($i * 1800);
                       $times = $b - time();
                       if($times < 1800){
                           $b = $b + 3600;
                       }
                       $arr[0][] = datetime($b, "H:i");
                   }else{
                       continue;
                   }
           }
       $this->success(__('获取成功'),['arr'=>$arr]);
   }

    /**
     * 附近技师
     */

    public function CityTechnician(){
        $uid=$this->auth->id;
        $page     = $this->request->request('page',1);//页数
        $serve_time=$this->request->request('serve_time');//服务时间
        $lon     = $this->request->request('lon',0);//经度
        $lat     = $this->request->request('lat',0);//纬度
        $sex      = $this->request->request('sex');//性别 0 女 1 男
        $fraction=$this->request->request('fraction');//好评高
        $order    = $this->request->request('order');//人气高
        $age_str   = $this->request->request('age');//最低年龄
        $store_id = $this->request->request('store_id');//服务商id
        $name = $this->request->request('name','');//技师姓名

        if ($lat == 'undefined') {
            $lat = 0;
        }
        if ($lon == 'undefined') {
            $lon = 0;
        }

        $where=[];
        if($age_str){
            list($age,$age1) = explode('-',$age_str);
            $where['technician_age']=["between",[$age,$age1]];
        }
        if($sex){
            if($sex==2){
              $sex=0;
            }
            $where['technician_gender'] = $sex;
        }
        if ($order) {
            if ($order == 1) {
                $order = 'order_count desc';
            } else {
                $order = 'order_count asc';
            }
        }
        if($fraction){
            if ($fraction == 1) {
                $order = 'technician_fraction desc';
            } else {
                $order = 'technician_fraction asc';
            }
        }
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        if($serve_time){
          $day=datetime($serve_time,'w');
          $where['work_day']=['like',"%$day"];
        }

        if($serve_time){
          $where['serve_time']=['<=',$serve_time];
          $where['serve_time_end']=['>=',$serve_time];
        }
        if($name){
            $where['technician_name']=['like',"%$name%"];
        }
        $technician= Db::name('technician')->where($where)->order($order)->page($page)->select();
        $info = [];
        if ($technician) {
            foreach ($technician as $item) {
                //可约时间
                $isset_order=\app\common\model\Order::where(['technician_id'=>$item['technician_id'],'order_status'=>['IN', [2,3,4,5]]])->find()??[];

                $store = Db::name('store')->where(['store_id' => $item['store_id']])->find();
                if($isset_order){
                    $earliestappionent_time = date('H:i', $isset_order['endserve_time']);
                }else{
                    $earliestappionent_time = date('H:i', time());
                }
                // 距离计算
                if ($item['lat'] == 0 || $item['lon'] == 0) {
                    $distance = '999km';
                } else {
                    $distance = distance($item['lon'], $item['lat'], $lon, $lat);
                }
                $label=Technicianlabel::where(['id'=>$item['technician_label']])->value('technician_label');
                $technician_address=mb_substr($item['technician_address'],0,2);
                $follow_status=Follow::where(['uid'=>$uid,'technician_id'=>$item['technician_id']])->field('follow_status')->find();
                if($follow_status&&$follow_status['follow_status']=1){
                    $follow_status='已关注';
                }else{
                    $follow_status='未关注';
                }
                $technician_images = [];
                if ($item['technician_images']) {
                    $technician_images = explode(',', $item['technician_images']);
                }
                $info[] = [
                    'technician_id' => $item['technician_id'],
                    'technician_name' => $item['technician_name'],
                    'technician_avatar' => $item['technician_avatar'],
                    'technician_gender' => $item['technician_gender'],
                    'order_count' => $item['order_count'],
                    'technician_age'=>$item['technician_age'],
                     'technician_label'=>$label,
                    'introduction'=>$item['introduction'],
                    'technician_images'=>$technician_images,
                    'technician_address'=>$technician_address,
                    'serve_time'=>$item['serve_time'],
                    'earliestappionent_time'=>$earliestappionent_time,
                    'lon' => $item['lon'],
                    'lat' => $item['lat'],
                    'follow_status'=>$follow_status,
                    'name' => $store['name'],
                    'store_id' => $store['store_id'],
                    'store_name' => $store['name'],
                    'distance' => $distance
                ];
            }
        }
        if ($lon != 0 && $lat != 0) {
            array_multisort(array_column($info,'distance'),SORT_ASC,$info);
        }
        $this->success('',$info);
    }






    /**
     * 技师实名认证
     */

    public function checkuser(){
        $uid=$this->auth->id;
        $postion=$this->auth->postion;
        if($postion!=2){
            $this->error('当前身份不是技师');
        }
        $idno=$this->request->request('idno');
        $identity_positive=$this->request->request('identity_positive');
        $identity_reverse=$this->request->request('identity_reverse');
        $real_name=$this->request->request('real_name','');
        $photo=$this->request->request('photo','');

        if(!$identity_positive|| !$identity_reverse ||!$idno){
            $this->error('参数不能为空');
        }
        $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
        if($technician){
            if($technician->is_check==1){
                $this->error('您已实名认证');
            }
        }

        $checkuser=Db::name('check_user')->where('uid','=',$uid)->find();
        if($checkuser&& $checkuser['status']==3){
           $this->error('实名信息正在审核中');
        }
        $data=[
            'idno'=>$idno,
            'uid'=>$uid,
            'real_name'=>$real_name,
            'identity_positive'=>$identity_positive,
            'photo'=>$photo,
            'identity_reverse'=>$identity_reverse,
            'status'=>3,
            'createtime'=>time()
        ];
        $res=Db::name('check_user')->insert($data);
        if($res){
            $this->success('认证成功,待审核');
        }else{
            $this->error('认证失败');
        }
    }

    /**
     * 技师设置资质
     */

    public function setLincense(){
        $uid=$this->auth->id;
        $license=$this->request->request('license','');
        $identity_positive=$this->request->request('identity_positive','');
        $identity_reverse=$this->request->request('identity_reverse','');
        $health_cards=$this->request->request('health_cards','');
        $other_images=$this->request->request('other_images','');
      $technician_image=$this->request->request('technician_image','');
        $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
        if($license){
            $technician->license=$license;
        }
        if($identity_positive){
            $technician->identity_positive=$identity_positive;
        }
        if($identity_reverse){
            $technician->identity_reverse=$identity_reverse;
        }
        if($health_cards){
            $technician->health_cards=$health_cards;
            $technician->comprehensive_scoring=$technician->comprehensive_scoring+1;
        }
        if($other_images){
            $technician->other_images=$other_images;
        }
      if($technician_image){
        $technician->technician_images=$technician_image;
      }
        $technician->save();
        $this->success('成功');
    }



    /**
     * 技师个人简介
     */
    public function setIntroduct(){
        $uid=$this->auth->id;
        $education_status=$this->request->request('education_status',0);// 0=保密,1=大专,2=本科,3=研究生,4=博士,5=博士后
        $nation=$this->request->request('nation','');//民族
        $height=$this->request->request('height',0);//身高
        $weight=$this->request->request('weight',0);//体重
        $age=$this->request->request('age',0);//年龄
        $introduction=$this->request->request('introduction','');//简介

        $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
        if($education_status){
            $technician->education_status=$education_status;
        }
        if($introduction){
            $technician->introduction=$introduction;
        }
        if($nation){
            $technician->nation=$nation;
        }
        if($height){
            $technician->height=$height;
        }
        if($weight){
            $technician->weight=$weight;
        }
        if($age){
            $technician->technician_age=$age;
        }
        $technician->save();
        $this->success('成功');
    }

    /**
     * 技师签到
     */

    public function sign(){
        $uid=$this->auth->id;
        $lon=$this->request->request('lon','');
        $lat=$this->request->request('lat','');
        $postion=$this->auth->postion;
        if($postion!=2){
            $this->error('当前身份不是技师');
        }
        $technician=\app\common\model\Technician::where('uid','=',$uid)->find();
        if($technician){
            if($lon){
                $technician->lon=$lon;
            }
            if($lat){
                $technician->lat=$lat;
            }
        }
        $technician->save();
        $res= \app\common\model\User::where(['id'=>$uid])->update(['is_signin'=>1,'signin_time'=>time()]);
        if($res){
            $this->success('签到成功');
        }else{
            $this->error('签到失败');
        }
    }


    /**
     * 技师设置服务区域
     */

    public function setServeScope(){
        $uid=$this->auth->id;
        $service_area=$this->request->request('service_area');
        $technician_address=$this->request->request('technician_address');
        $serve_scope=$this->request->request('serve_scope');
        $lon=$this->request->request('lon');
        $lat=$this->request->request('lat');
        $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
        if($technician){
            if($service_area){
               $technician->service_area=$service_area;
            }
            if($technician_address){
                $technician->technician_address=$technician_address;
            }
            if($serve_scope){
                $technician->serve_scope=$serve_scope;
            }
            if($lon){
                $technician->lon=$lon;
            }
            if($lat){
                $technician->lat=$lat;
            }
            $technician->save();
        }
        $this->success('成功');
    }


    /**
     * 技师修改头像
     */


    public function updAvatar(){
        $uid=$this->auth->id;
        $avatar=$this->request->request('avatar','');
        \app\common\model\User::where(['id'=>$uid])->update(['avatar'=>$avatar]);
        \app\common\model\Technician::where(['uid'=>$uid])->update(['technician_avatar'=>$avatar]);
        $this->success('头像更新成功');
    }


    /**
     * 提现记录
     */

    public function withdraw(){
        $uid=$this->auth->id;
        $position=$this->auth->position;
        $withdraw=Withdraw::where(['user_id'=>$uid,'position'=>$position])->field('id,amount,type,status,createtime,memo')->select();
        $this->success('',$withdraw);
    }

    /**
     * 服务项目
     */

    public function ServeAll(){
        $uid=$this->auth->id;
        $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();

        $servec=Servec::field('id,cname,cimage')->select();
        foreach ($servec as $item){
          $item['count']=\app\admin\model\Serve::where(['store_id'=>$technician['store_id'],'serve_status'=>1,'serve_cid'=>$item['id']])->count();
          $item['add_count']=TechnicianServe::where(['serve_cid'=>$item['id'],'technician_id'=>$technician['technician_id'],'status'=>1,'store_id'=>$technician['store_id']])->count()??0;
        }
        $this->success('',$servec??[]);
    }

    /**
     * 添加服务项目
     */

    public function addServe(){
        $uid=$this->auth->id;
        $serve_id=$this->request->request('serve_id');
        $serve=\app\common\model\Serve::where(['is_added'=>0,'serve_status'=>1,'id'=>$serve_id])->find();
        $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
        $store_id=$technician->store_id;
        if($serve){
           $tserve=TechnicianServe::where(['serve_id'=>$serve_id,'store_id'=>$store_id])->find();
           if($tserve){
               $tserve->technician_id=$technician->technician_id;
               $tserve->save();
           }
        }
        $this->success('操作成功');
    }

  /**
   * 服务分类下面的服务项目
   */

    public function Servecname(){
      $uid=$this->auth->id;
      $cid=$this->request->request('cid'); //分类id
      ## 技师信息
      $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
      ## 店铺信息
      $store_id=$technician['store_id'];
      $store=\app\common\model\Store::where(['store_id'=>$store_id])->find();

      //商家下面服务
      $tserve=TechnicianServe::where(['store_id'=>$store_id,'serve_cid'=>$cid,'status'=>1])
          ->group('serve_id')->column('serve_id');
      // 店铺全部服务信息
      $serve=\app\common\model\Serve::where(['id'=>['in',$tserve],'serve_status'=>1,'is_added'=>0])->field('id,serve_image,serve_name,serve_price')->select();

      //技师下面服务
      $te=TechnicianServe::where(['technician_id'=>$technician['technician_id'],'serve_id'=>['in',$tserve],'status'=>1])->column('serve_id');


      ## 循环全部服务，判断服务是否存在技师所选服务id集合中
      foreach ($serve as $key =>$value){
          $value['store']=$store->name;
          $value['check_status'] = in_array($value['id'],$te)?'1':'2';
      }
      $this->success('',$serve??[]);

    }


    /**
     * 服务详情
     */

    public function ServeDetail(){
        $serve_id=$this->request->request('serve_id');
        $serve=\app\common\model\Serve::where(['id'=>$serve_id])->field('serve_name,id,serve_details,serve_price,serve_duration,serve_image,sales')->find();
        $this->success('查询成功',$serve);
    }


    /**
     * 设置服务时间
     */

    public function SetServetime(){
        $uid=$this->auth->id;
        $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
        $serve_time=$this->request->request('serve_time','');
        $serve_time_end=$this->request->request('serve_time_end','');
        $work_day=$this->request->request('work_day','');
      $work_day=$work_day?html_entity_decode($work_day):'';
      if($technician){
        if( $serve_time){
          $technician->serve_time=$serve_time;
        }
        if($serve_time_end){
          $technician->serve_time_end=$serve_time_end;
        }
        if($work_day){
          $technician->work_day=$work_day;
        }
      }
      $result=$technician->save();
        if($result){
          $this->success('设置成功');
        }else{
          $this->error('设置失败');
        }

    }

    /**
     * 加入商家
     */
    public function joinstore(){
      $uid=$this->auth->id;
      $code=$this->request->request('code');
      $postion=$this->auth->postion;
      if($postion!=2){
        $this->error('当前身份不是技师');
      }
      $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
      $store=\app\common\model\Store::where(['store_code'=>$code])->find();
      if(!$store){
        $this->error('要加入的商家不存在');
      }
      if($technician){
        $technician->store_id=$store->store_id;
        $technician->is_join_store=1;
        $technician->save();
      }
      $this->success('加入商家成功');
    }


    /**
     * 商家广场
     */

    public function storesquare(){
      $uid=$this->auth->id;
      $is_telephone=$this->request->request('is_telephone');//是否允许未加入商家电话联系 0 开启 1 关闭 默认关闭
      \app\common\model\Technician::where(['uid'=>$uid])->update(['is_telephone'=>$is_telephone]);
      $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
      $store=\app\admin\model\store\Store::field('store_id,name,store_introduction,uid')->select();
      foreach ($store as $item){
        $tserve=TechnicianServe::where(['store_id'=>$item['store_id'],'status'=>1])->group('serve_id')->column('serve_id');
        $serve=\app\common\model\Serve::where(['id'=>['in',$tserve],'serve_status'=>1,'is_added'=>0])->field('id,serve_image,serve_name')->find();
        if($serve){
            $item['serve_name']=$serve['serve_name'];
            $item['serve_image']=$serve['serve_image'];
            if(!$item['serve_image']){
                $item['serve_image']=config('site.logo');
            }
            $item['serve_id']=$serve['id'];
            $item['is_telephone']=$technician['is_telephone']??0;
        }
      }
      $this->success('',$store);
    }

  /**
   * 商家广场中的服务详情
   */

  public function store_serveDetail(){
    $uid=$this->auth->id;
    $serve_id=$this->request->request('serve_id');
    if(!$serve_id){
      $this->error('参数不能为空');
    }
    $serve=\app\common\model\Serve::where(['id'=>$serve_id])->field('id,serve_name,createtime,serve_image')->find();
    $serve['createtime']=date('Y-m-d H:i',$serve->createtime);
    $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
    $store=\app\admin\model\Store::where(['store_id'=>$technician->store_id])->field('store_id,name,createtime,store_introduction')->find();
    $serve['store']=$store;
    $serve['store']['createtime']=date('Y-m-d',$store->createtime);
    $this->success('',$serve);

  }

    /**
     * 技师设置或取消忙时
     */

    public function setBusyhours(){
      $uid=$this->auth->id;
      $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
      $type=$this->request->request('type');//1 设置忙时,2取消忙时
      $detail = $this->request->request('detail','');
      $details=$detail?html_entity_decode($detail):'';
      if(!empty($details)){
        $details = trim($details);
      }
      if($type==1){
        $data=[
          'technician_id'=>$technician->technician_id,
          'detail'=>$details,
        ];
        $busy_hours=BusyHours::where(['technician_id'=>$technician->technician_id])->find();
        if(!$busy_hours){
            $result=Db::name('busy_hour')->insert($data);
        }else{
            $result=$busy_hours->save($data);
        }
        if($result){
          $this->success('设置成功');
        }else{
          $this->error('设置失败');
        }
      }else if($type==2){
        $id=$this->request->request('id');
        $result=Db::name('busy_hour')->where(['id'=>$id])->update(['detail'=>$details]);
        if($result){
          $this->success('取消成功');
        }else{
          $this->error('取消失败');
        }
      }
    }


  /**
   * 查看当前技师忙时、闲时
   */

  public function showbusyhour(){
    $uid=$this->auth->id;
    $postion=$this->auth->postion;
    $date=$this->request->request('date');
    $technician_id=$this->request->request('technician_id');
    $list=[];
    if($postion==2){
      $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
      $list=Db::name('busy_hour')->where(['technician_id'=>$technician['technician_id'],'detail'=>['like',"%$date%"]])->find();
    }else if($postion==3){
      $technician=\app\admin\model\Technician::where(['technician_id'=>$technician_id])->find();
      $list=Db::name('busy_hour')->where(['technician_id'=>$technician['technician_id'],'detail'=>['like',"%$date%"]])->find();
    }
        $arr = json_decode($list['detail']) ?? [];
        $list['detail'] = $arr;
        if ($arr) {
            $this->success('', $list);
        } else {
            $this->success('', $list);
        }
  }

  /**
   * 查询时间段
   */

  public function time_quantum(){
    $list=Quantum::all();
    foreach ($list as $item){
        $item['status']=2;
    }
    $this->success('',$list);
  }

  /**
   * 获取最近六天日期
   */

  public function getDate(){

    $dayArr = [];


    for($j = 0; $j < 6; $j++){
      $day = datetime(time() + (24 * 3600 * $j), "Y-m-d");

      $dayArr[$j] = $day;
    }
    $this->success('获取成功',$dayArr);
  }


    /**
     * 技师排行榜
     * 按照总评分进行排行,每周周一刷新一次
     */

    public function technicianRanking(){
        $uid=$this->auth->id;
        //当前技师评分
        $tech=\app\common\model\Technician::where(['uid'=>$uid])->field('comprehensive_scoring')->find();
      $technician=\app\common\model\Technician::field('technician_id,technician_name,technician_avatar,comprehensive_scoring')->order('comprehensive_scoring','desc')->select();
      $time=60*60*24*7;
        $scoring_rules=config('site.scoring_rules');
        $item_count=\app\admin\model\Technician::where('comprehensive_scoring','<',$tech)->count();//小于当前技师评分的人数
        if($item_count==0){
            $item_count=2;
        }
        $zcount=\app\admin\model\Technician::count();//技师总人数
      foreach ($technician as $item){
          $item['tech']=$tech['comprehensive_scoring'];
          $item['technician_avatar']=cdnurl($item['technician_avatar']);
          $item['scoring_rules']=$scoring_rules;//评分规则
          $item['zcount']=(bcdiv($item_count,$zcount,2)?:0).'%';//超越了多少同行
      }
      Cache::set('technician',$technician,$time);
      if(!Cache::get('technician')){
        Cache::set('technician',$technician,$time);
      }else{
       $this->success('', $technician??[]);
      }
    }


    /**
     * 技师个人中心
     */
    public function technician_index(){
      $uid=$this->auth->id;
      $technician=\app\common\model\Technician::where('uid','=',$uid)->field('technician_id,technician_name,real_name,is_check,turnover,chargeback_quantity,repurchase_num,comprehensive_scoring,license,health_cards')->find();
      $checkuser=CheckUser::where(['uid'=>$uid])->field('photo')->find();
      $technician['photo']=$checkuser['photo'];
      $log=MoneyLog::where(['is_clearing'=>1,'postion'=>2,'user_id'=>$uid])->whereTime('createtime','month')->count();//本月已结账
      $settled=MoneyLog::where(['is_clearing'=>0,'postion'=>2,'user_id'=>$uid])->count();//已支付待结算
      $technician['log']=$log;
      $technician['settled']=$settled;
      $money=\app\common\model\User::where(['id'=>$uid,'postion'=>2])->find();
      $technician['money']=$money->money??0;
      $this->success('',$technician);
    }


    /**
     * 是否允许未加入商家电话联系我
     * is_telephone 0 开启 1 关闭
     */
    public function store_tephone(){
        $uid=$this->auth->id;
        $is_telephone=$this->request->request('is_telephone',0);//0 开启 1 关闭
      \app\common\model\Technician::where(['uid'=>$uid])->update(['is_telephone'=>$is_telephone]);
        $technician=\app\common\model\Technician::where(['uid'=>$uid])->field('is_telephone,technician_id')->find();
        $this->success('',$technician);
    }

    /**
     * 技师首页
     */


    public function technician_home(){
      $uid=$this->auth->id;
      $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
      $intr=\app\common\model\Technician::where(['uid'=>$uid])->field('education_status,nation,height,weight,introduction')->find();
      $list=[];
      $list['avatar']=$technician['technician_avatar']??[];
        if($intr['education_status']||$intr['nation']||$intr['height']||$intr['weight']||$intr['introduction']){
            $list['intr_status']=1;
        }else{
            $list['intr_status']=2;
        }
      $list['phone']=$technician['technician_phone']??[];
        $list['introduction']=$technician['introduction']??[];
      $list['real_name']=$technician['real_name']??'';
      $list['technician_address']=$technician['technician_address'];
      $list['star']=$technician['comprehensive_scoring'];
      $tserve=TechnicianServe::where(['technician_id'=>['in',$technician['technician_id']],'store_id'=>$technician['store_id'],'status'=>1])->count();
      $list['serve_count']=$tserve;
      $list['is_check']=$technician['is_check'];
      $order=\app\common\model\Order::where(['technician_id'=>$technician['technician_id'],'order_status'=>1])->count();
      $jinxing=\app\common\model\Order::where(['technician_id'=>$technician['technician_id'],'order_status'=>['in',[2,3,4,5]]])->count();
      $finish=\app\common\model\Order::where(['technician_id'=>$technician['technician_id'],'order_status'=>['in',[6,8]]])->count();
      $list['addcount']=$order;
      $list['jx_count']=$jinxing;
      $list['finish']=$finish;
      $this->success('',$list);
    }

    /**
     * 收入明细
     */


    public function technician_money(){
      $uid=$this->auth->id;
      $postion=$this->auth->postion;
      $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
      $types=$this->request->request('types');//0 待结算 1 已结算
      $times=$this->request->request('times');
      $time=$this->request->request('time');
      $where=[];
      $where['user_id']=$uid;
      if($types){
        $where['is_clearing']=$types;
      }
      if($postion){
        $where['postion']=$postion;
      }
      if($times||$time){
        $where['createtime']=['between',[$times,$time]];
        $list=MoneyLog::where($where)->field('user_id,id,memo,money,type,is_clearing,postion,createtime')->order('createtime','desc')->select();
          foreach ($list as $item){
              $item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
              $item['yjsuan']=MoneyLog::where(['user_id'=>$uid,'postion'=>2,'is_clearing'=>1])->whereTime(['createtime'=>['between',[$times,$time]]])->sum('money');
              $item['wjsuan']=MoneyLog::where(['user_id'=>$uid,'postion'=>2,'is_clearing'=>0])->whereTime(['createtime'=>['between',[$times,$time]]])->sum('money');
          }
      }else{
        $list=MoneyLog::where($where)->field('user_id,id,memo,money,type,is_clearing,postion,createtime')->whereTime('createtime','month')->order('createtime','desc')->select();
          foreach ($list as $item){
              $item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
              $item['yjsuan']=MoneyLog::where(['user_id'=>$uid,'postion'=>2,'is_clearing'=>1])->whereTime('createtime','month')->sum('money');
              $item['wjsuan']=MoneyLog::where(['user_id'=>$uid,'postion'=>2,'is_clearing'=>0])->whereTime('createtime','month')->sum('money');
          }
      }
      $this->success('',$list);
    }


    /**
     * 订单统计
     */


    public function Ordercensus(){
      $uid=$this->auth->id;
      $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
      $times=$this->request->request('times');
      $types=$this->request->request('types');//1 近3日 2 近7日 3 近30日
      $list=[];
      if($types==1){
          $list['finishcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','-3 days')->count();//已完成订单数
          $list['finishmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','-3 days')->sum('order_amount')??0;//已完成订单金额
          $list['jxcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->whereTime('beginserve_time','-3 days')->count();//进行中订单数
          $list['jxmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->whereTime('beginserve_time','-3 days')->sum('order_amount');//进行中订单金额
          $list['cancelcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->whereTime('cancel_time','-3 days')->count();//已取消订单数
          $list['cancelmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->whereTime('cancel_time','-3 days')->sum('order_amount');//已取消订单金额
      }else if($types==2){
          $list['finishcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','-7 days')->count();//已完成订单数
          $list['finishmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','-7 days')->sum('order_amount');//已完成订单金额
          $list['jxcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->whereTime('beginserve_time','-7 days')->count();//进行中订单数
          $list['jxmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->whereTime('beginserve_time','-7 days')->sum('order_amount');//进行中订单金额
          $list['cancelcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->whereTime('cancel_time','-7 days')->count();//已取消订单数
          $list['cancelmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->whereTime('cancel_time','-7 days')->sum('order_amount');//已取消订单金额
      }else if($types==3){
          $list['finishcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','-30 days')->count();//已完成订单数
          $list['finishmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','-30 days')->sum('order_amount');//已完成订单金额
          $list['jxcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->whereTime('beginserve_time','-30 days')->count();//进行中订单数
          $list['jxmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->whereTime('beginserve_time','-30 days')->sum('order_amount');//已完成订单金额
          $list['cancelcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->whereTime('cancel_time','-30 days')->count();//已取消订单数
          $list['cancelmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->whereTime('cancel_time','-30 days')->sum('order_amount');//已取消订单金额
      }

$where=[];
      if($times){
          $time=strtotime($times);
        $where['endserve_time']=$time;
          $list['count']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->count();//根据时间筛选完成订单数
          $list['mileagefee']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->sum('mileagefee')??0;//路费
          $list['plusitem_money']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->where($where)->sum('plusitem_money')??0;//加项补差价金额
          $list['serve_price']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->where($where)->sum('serve_price')??0;//项目费用
          $day=time();
          if(date('Y-m-d',$day)==date('Y-m-d')){
              $list['count']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','today')->count();//今日已完成订单数
              $list['mileagefee']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','today')->sum('mileagefee')??0;//路费
              $list['plusitem_money']=Addclock::where(['technician_id'=>$technician->technician_id,'status'=>1])->whereTime('createtime','today')->sum('price')??0;//加项补差价金额
              $list['serve_price']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->whereTime('endserve_time','today')->sum('serve_price')??0;//项目费用
          }
          $list['finishcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->where($where)->count();//已完成订单数
          $list['finishmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->where($where)->sum('order_amount');//已完成订单金额
          $list['jxcount']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->count();//进行中订单数
          $list['jxmoney']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->sum('order_amount');//进行中订单金额
          $list['cancelcount']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->count();//取消订单数
          $list['cancelmoney']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->sum('order_amount');//取消订单金额
      }
        $list['finishcount']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->count();//已完成订单数
        $list['finishmoney']=\app\common\model\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[6,8]]])->sum('order_amount');//已完成订单金额
        $list['jxcount']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->count();//进行中订单数
        $list['jxmoney']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[2,3,4,5,14]]])->sum('order_amount');//进行中订单金额
        $list['cancelcount']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->count();//取消订单数
        $list['cancelmoney']=\app\admin\model\order\Order::where(['technician_id'=>$technician->technician_id,'order_status'=>['in',[7,11,12,13]]])->sum('order_amount');//取消订单金额
    $this->success('',$list);
    }



    /**
     * 技师下面的评价
     */

    public function technician_comment(){
      $uid=$this->auth->id;
      $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
      $type=$this->request->request('type');//1 好评 2 中评 3 差评 4 有图
      $where=[];
      if($type){
        $where['type']=$type;
      }
      if($technician){
        $where['technician_id']=$technician->technician_id;
      }
      $comment=OrderComment::where($where)->field('order_id,id,star,uid,content,image,createtime,technician_id,store_id,serve_id,address')->select();
      $haoping=OrderComment::where(['star'=>5,'technician_id'=>$technician->technician_id])->count();
      $zhongping=OrderComment::where(['star'=>['in',[2,3,4]],'technician_id'=>$technician->technician_id])->count();
      $chaping=OrderComment::where(['star'=>1,'store_id'=>$technician->technician_id])->count();
      $quanbu=OrderComment::where(['technician_id'=>$technician->technician_id])->count();
      $youtu=OrderComment::where(['technician_id'=>$technician->technician_id,'type'=>4])->count();
      foreach ($comment as $item){
        $item['image']=explode(',',$item['image']);
        $item['haoping']=$haoping;
        $item['zhongping']=$zhongping;
        $item['chaping']=$chaping;
        $item['quanbu']=$quanbu;
        $item['youtu']=$youtu;
        $item['user']=\app\common\model\User::where(['id'=>$item['uid'],'postion'=>1])->field('id,avatar,nickname')->find();
        $item['technician']=\app\admin\model\Technician::where(['technician_id'=>$technician->technician_id])->field('technician_id,technician_name,technician_avatar')->find();
        $item['serve']=\app\admin\model\Serve::where(['id'=>$item['serve_id'],'serve_status'=>1,'is_added'=>0])->field('id,serve_name')->find();
        $zan=CommentZan::where(['uid'=>$uid,'postion'=>2])->find();
        if(!$zan) {
          $item['zan']='未点赞';
        }else{
          if($zan->zan==1){
            $item['zan']='已点赞';
          }
        }
        $item['zancount']=CommentZan::where(['comment_id'=>$item['id'],'zan'=>1])->count();
      }
      $this->success('',$comment);

    }


    /**
     * 已经加入商家
     */

    public function technician_store(){
      $uid=$this->auth->id;
      $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
          if ($technician) {
              if($technician->store_id!=0) {
                  $store = \app\common\model\Store::where(['store_id' => $technician->store_id])->field('store_id,name,createtime,image,uid')->find();
                  $store['createtime'] = date('Y-m-d', $store->createtime);
                  if (!$store->image) {
                      $store['image'] = config('site.default_avatar');
                  }
                  $this->success('查询成功', $store);
              }
          } else {
              $this->error('该账号不是技师', '',401);
          }
    }

    /**
     * 技师主页
     */
    public function tehome(){

      $uid=$this->auth->id;
      $url=$this->auth->invite_url;
      $list=[];
      $checkUser=CheckUser::where(['uid'=>$uid,'status'=>1])->find();
      $photo='';
      if($checkUser){
        $photo=$checkUser->photo;
      }
      $list['photo']=$photo;
      $list['url']=$url;
      $this->success('',$list);
    }

    /**
     * 查看当前技师资质
     */

    public function showlicense(){
      $uid=$this->auth->id;
      $technician=\app\admin\model\Technician::where(['uid'=>$uid])->field('license,identity_positive,identity_reverse,health_cards,other_images,technician_images')->find();
      if($technician){
          $technician['technician_images']=explode(',',$technician['technician_images']);
      }
        $this->success('',$technician??[]);

    }

    /**
     * 服务时间下面的可选择技师
     */

    public function timetechnician(){
        $time=$this->request->request('time');
        $serve_id=$this->request->request('serve_id');
        $lon=$this->request->request('lon');
        $lat=$this->request->request('lat');
        $times = strtotime($time);
        $timey=datetime($times,'H:i');
        $week = date('w', $times);
        $tserve=TechnicianServe::where(['serve_id'=>['in',$serve_id]])->group('technician_id')->column('technician_id');
        $info= \app\common\model\Technician::where(['technician_id'=>['in',$tserve],'work_day'=>['like',"%$week%"]])->field('technician_id,technician_avatar,lon,lat,serve_time,serve_time_end')->select();
        if($info) {
            $technician = \app\common\model\Technician::where('serve_time', '<', $timey)->where('serve_time_end', '>=', $timey)->field('technician_id,uid,technician_avatar,lon,lat,serve_time,serve_time_end,store_id,feedback,weight,introduction,nation,education_status,height,zodiak,comment_count,technician_fraction,technician_gender,technician_age,technician_name')->select();
            foreach ($technician as $item) {
                $item['distance'] = distance($lon, $lat, $item['lon'], $item['lat']);
                $store = \app\common\model\Store::where(['store_id' => $item['store_id']])->field('store_id,name')->find();
                if ($store) {
                    $item['store'] = $store;
                } else {
                    $item['store'] = '暂未绑定店铺';
                }
                if ($item['education_status'] == 0) {
                    $item['education_status'] = '保密';
                } else if ($item['education_status'] == 1) {
                    $item['education_status'] = '大专';
                } else if ($item['education_status'] == 2) {
                    $item['education_status'] = '本科';
                } else if ($item['education_status'] == 3) {
                    $item['education_status'] = '研究生';
                } else if ($item['education_status'] == 4) {
                    $item['education_status'] = '博士';
                } else if ($item['education_status'] == 5) {
                    $item['education_status'] = '博士后';
                }
                $this->success('', $technician);
            }
        }else{
            $this->success('', $info??[]);
        }
    }

    /**
     * 获取预约时间
     */
    public function getServeTime(){
        $arr    = [];
        $dayArr = [];
        for($j = 0; $j < 6; $j++){
            $day = datetime(time() + (24 * 3600 * $j), "Y-m-d");
            for($i = 0; $i < 48; $i++){
                if($j == 0){
                    $beginTime = mktime(0, 0, 0, date("m"), date("d"), date("y"));
                    if(intval(datetime(time(), "H")) * 2<= $i){
                        $b     = $beginTime + ($i * 1800);
                        $times = $b - time();
                        if($times < 1800){
                            $b = $b + 3600;
                        }
                        $arr[0][] = datetime($b, "H:i");
                    }else{
                        continue;
                    }
                }else{
                    $beginTime   = mktime(0, 0, 0, date("m"), date("d") + $j, date("y"));
                    $b           = $beginTime + ($i * 1800);
                    $arr[$j][$i] = datetime($b, "H:i");
                }
            }
            $dayArr[$j] = $day;
        }
        $this->success(__('获取成功'), [$dayArr, $arr]);
    }

    /**
     * 查询当前基础认证是否认证
     */

    public function technician_check(){
       $uid=$this->auth->id;
       $technician=\app\common\model\Technician::where(['uid'=>$uid])->field('is_check,health_cards,is_avatar,technician_id')->find();
        if($technician->is_check==1){
            $technician['is_check']=1;
        }else{
            $technician['is_check']=0;
        }
       if($technician->health_cards){
          $technician['is_health']=1;
       }else{
           $technician['is_health']=0;
       }
       if($technician->is_avatar==1){
           $technician['is_avatar']=1;
       }else{
           $technician['is_avatar']=0;
       }
       $this->success('查询成功',$technician);
    }


    /**
     * @throws \think\exception\DbException
     * 认证头像相似
     */
    public function editavatar(){
     $uid=$this->auth->id;
     $postion=$this->auth->postion;
     if(!$postion){
         $this->error('当前身份不是技师');
     }
     $technician=\app\common\model\Technician::get(['uid'=>$uid]);
     $technician->is_avatar=1;
     $technician->comprehensive_scoring=$technician['comprehensive_scoring']+1;
     $technician->save();
     $this->success('更新成功');
    }

    /**
     * 查看当前技师服务范围
     */

      public function showsign(){
        $uid=$this->auth->id;
        $user=\app\common\model\User::where(['id'=>$uid])->whereTime('signin_time','today')->find();
        $technician=\app\admin\model\Technician::where(['uid'=>$uid])->field('lon,lat,technician_id,technician_address,service_area,serve_scope')->find();
        if($user['is_signin']==1){
         $technician['sign']='已签到';
        }else{
            $technician['sign']='未签到';
        }
        $this->success('查询成功',$technician);
      }


    /**
     * 重置签到状态
     */

    public function resetSign(){
        $sign=\app\admin\model\User::where(['is_signin'=>1])->select();
        if($sign){
            foreach ($sign as $item){
                $item['is_signin']=0;
                $item->save();
            }
        }
        $this->success('成功');

    }

      /**
       * 查询当前技师是否开启电话联系
       */

      public function is_tephone(){
          $uid=$this->auth->id;
          $technician=\app\common\model\Technician::where(['uid'=>$uid])->field('is_telephone')->find();
          if(!$technician){
              $technician['is_telephone']=1;
          }
          $this->success('查询成功',$technician);
      }

      /**
       * 查询技师所在的店铺
       */
      public function techstore(){
//          $uid=$this->auth->id;
          $store_id=$this->request->request('store_id');
//          $technician=\app\common\model\Technician::where(['uid'=>$uid])->find();
          if($store_id){
              $store=\app\admin\model\Store::where(['store_id'=>$store_id])->field('name,image,lincense,company_name,store_grade')->find();
             $store->store_grade=intval($store->store_grade);
          }
          $tserve=TechnicianServe::where(['store_id'=>$store_id,'status'=>1])->group('serve_id')->column('serve_id');
          $serve=\app\admin\model\Serve::where(['id'=>['in',$tserve]])->field('id,serve_name,serve_image,serve_price,sales,serve_duration')->select();
          $store['serve']=$serve;
          $this->success('查询成功',$store);

      }


      /**
       * 查看当前技师服务时间
       */
      public function showServetime(){
          $uid=$this->auth->id;
          $technician=\app\admin\model\Technician::where(['uid'=>$uid])->field('work_day,serve_time,serve_time_end')->find();
          $this->success('查询成功', $technician);
      }

      /**
       * 查看当前技师实名认证信息
       */
      public function showcheck(){
          $uid=$this->auth->id;
          $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
          if($technician){
              $checkuser=CheckUser::where(['uid'=>$technician['uid']])->field('real_name,idno,identity_positive,identity_reverse,photo,status')->find();
              if($checkuser){
                  $checkuser['real_name']=$checkuser->real_name;
                  $checkuser['idno']=$checkuser->idno;
                  $checkuser['identity_positive']=$checkuser->identity_positive;
                  $checkuser['identity_reverse']=$checkuser->identity_reverse;
                  $checkuser['photo']=$checkuser->photo;
                  $checkuser['status']=$checkuser->status;
              }
          }else{
              $checkuser=[];
          }
          $this->success('查询成功',$checkuser);
      }

      /**
       * 技师取消商家服务
       */

      public function cancelserve(){
          $serve_id=$this->request->request('serve_id');
          $uid=$this->auth->id;
          $technician=\app\admin\model\Technician::where(['uid'=>$uid])->find();
          if($technician->store_id==0){
              $this->error('当前技师还未加入商家');
          }
          $tserve=TechnicianServe::where(['store_id'=>$technician->store_id,'serve_id'=>$serve_id])->find();
          if(!$tserve){
              $this->error('当前商家没有该服务');
          }
          TechnicianServe::where(['serve_id'=>$serve_id,'technician_id'=>$technician->technician_id])->update(['technician_id'=>0]);
          $this->success('修改成功');
      }

      /**
       * 查看技师个人简介
       */

      public function showintrouct(){
          $uid=$this->auth->id;
          $technician=\app\admin\model\Technician::where(['uid'=>$uid])->field('nation,height,introduction,weight,education_status,technician_age')->find();
          if($technician['education_status']==0){
              $technician['education_status']='保密';
          }else if($technician['education_status']==1){
              $technician['education_status']='大专';
          }else if($technician['education_status']==2) {
              $technician['education_status'] = '本科';
          }
          else if($technician['education_status']==3) {
              $technician['education_status'] = '研究生';
          }else if($technician['education_status']==4) {
              $technician['education_status'] = '博士';
          }else if($technician['education_status']==5) {
              $technician['education_status'] = '博士后';
          }
          $this->success('查询成功',$technician);

      }
}